package com.db.compare.core;

import com.db.compare.model.RbacUser;
import com.db.compare.utils.SecurityUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public abstract class BaseController {

    /**
     * 返回成功信息
     *
     * @return
     */
    public static BaseResponse buildSuccessResponse() {
        BaseResponse response = new BaseResponse();
        response.setSuccess(true);
        return response;
    }



    /**
     * 返回成功信息
     *
     * @param data
     * @return
     */
    public static <T> BaseResponse<T> buildSuccessResponse(T data) {

        BaseResponse<T> response = new BaseResponse<T>();
        response.setSuccess(true);
        
        if(data instanceof String || data instanceof Integer ||data instanceof Double ||data instanceof Short
                ||data instanceof Character || data instanceof Boolean ||data instanceof Float || data instanceof Long){
            Map<String,Object> params=new HashMap<>();
            params.put("key",data);
            response.setData((T) params);
        }else{
            response.setData(data);
        }
        return response;
    }

    public static <T> BaseResponse<T> buildSuccessResponse(T data,String accessToken) {
        BaseResponse<T> response=buildSuccessResponse(data);
        response.setAccessToken(accessToken);
        return response;
    }

    /* -------------------- 构建返回结果 - 失败 -------------------- */

    /**
     * 返回失败信息
     *
     * @return
     */
    public static BaseResponse buildFailResponse() {
        BaseResponse response = new BaseResponse();
        response.setSuccess(false);
        return response;
    }

    /**
     * 返回失败信息
     *
     * @return
     */
    public static BaseResponse buildFailResponse(Exception e) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(false);
        response.setMessage(e.getMessage());
        response.setThrowable(e.getClass().getName());
        
        return response;
    }

    /**
     * 返回失败信息
     *
     * @param message
     * @return
     */
    public static BaseResponse buildFailResponse(String message) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(false);
        response.setMessage(message);
        return response;
    }

    /**
     * 返回失败信息
     *
     * @param code
     * @param message
     * @return
     */
    public static BaseResponse buildFailResponse(Integer code, String message) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(false);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 返回成功信息--返回提示信息
     *
     * @param code
     * @param message
     * @return
     */
    public static BaseResponse buildSuccessResponse(Integer code, String message) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(true);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }
    public static <T> BaseResponse<T> buildSuccessResponse(T data,String accessToken,int code,String message) {
        BaseResponse<T> response=buildSuccessResponse(data);
        response.setAccessToken(accessToken);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    /**
     * 返回成功信息--带对象
     *
     * @param code
     * @param message
     * @return
     */
    public static <T> BaseResponse<T> buildSuccessResponse(Integer code, String message,T data) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(true);
        response.setCode(code);
        response.setMessage(message);
        response=buildSuccessResponse(data);
        return response;
    }

    /**
     * 返回失败信息
     *
     * @param code
     * @param message
     * @param path
     * @param throwable
     * @return
     */
    public static BaseResponse buildFailResponse(Integer code, String message, String path, String throwable) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(false);
        response.setCode(code);
        response.setMessage(message);
        response.setPath(path);
        response.setThrowable(throwable);
        return response;
    }

    public static BaseResponse buildSuccessOrFailResponse(Integer code, String message, Boolean flag) {
        BaseResponse response = new BaseResponse();
        response.setSuccess(flag);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }
}
